namespace TSL.Utility.Json
{
    using System;
    using System.Collections;
    using System.Configuration;

    public sealed class Utility
    {
        internal static bool ConverterRegistered = false;
        private static AjaxSettings m_Settings = null;
        private static object m_SettingsLock = new object();

        internal static void AddConverter(AjaxSettings settings, IJavaScriptConverter converter)
        {
            AddConverter(settings, converter, false);
        }

        internal static void AddConverter(AjaxSettings settings, IJavaScriptConverter converter, bool replace)
        {
            Type type;
            int num;
            for (num = 0; num < converter.SerializableTypes.Length; num++)
            {
                type = converter.SerializableTypes[num];
                if (settings.SerializableConverters.ContainsKey(type))
                {
                    if (replace)
                    {
                        settings.SerializableConverters[type] = converter;
                    }
                }
                else
                {
                    settings.SerializableConverters.Add(type, converter);
                }
            }
            for (num = 0; num < converter.DeserializableTypes.Length; num++)
            {
                type = converter.DeserializableTypes[num];
                if (settings.DeserializableConverters.ContainsKey(type))
                {
                    if (replace)
                    {
                        settings.DeserializableConverters[type] = converter;
                    }
                }
                else
                {
                    settings.DeserializableConverters.Add(type, converter);
                }
            }
        }

        internal static void AddDefaultConverter(AjaxSettings settings)
        {
            AddConverter(settings, new StringConverter());
            AddConverter(settings, new PrimitiveConverter());
            AddConverter(settings, new GuidConverter());
            AddConverter(settings, new ExceptionConverter());
            AddConverter(settings, new EnumConverter());
            AddConverter(settings, new DecimalConverter());
            AddConverter(settings, new NameValueCollectionConverter());
            AddConverter(settings, new DateTimeConverter());
            AddConverter(settings, new DataSetConverter());
            AddConverter(settings, new DataTableConverter());
            AddConverter(settings, new DataViewConverter());
            AddConverter(settings, new IJavaScriptObjectConverter());
            AddConverter(settings, new ProfileBaseConverter());
            AddConverter(settings, new IDictionaryConverter());
            AddConverter(settings, new IListConverter());
            AddConverter(settings, new HashtableConverter());
            AddConverter(settings, new IEnumerableConverter());
            AddConverter(settings, new DataRowConverter());
            AddConverter(settings, new HtmlControlConverter());
        }

        [Obsolete("The recommended alternative is to add the converter type to ajaxNet/ajaxSettings/jsonConverters.", false)]
        public static void RegisterConverterForAjax(IJavaScriptConverter converter)
        {
            AddConverter(Settings, converter);
        }

        internal static void RemoveConverter(AjaxSettings settings, Type t)
        {
            Type current;
            bool flag = false;
            IEnumerator enumerator = settings.SerializableConverters.Keys.GetEnumerator();
            while (!flag && enumerator.MoveNext())
            {
                current = (Type) enumerator.Current;
                if (settings.SerializableConverters[current].GetType() == t)
                {
                    settings.SerializableConverters.Remove(current);
                    flag = true;
                }
            }
            flag = false;
            enumerator = settings.DeserializableConverters.Keys.GetEnumerator();
            while (!flag && enumerator.MoveNext())
            {
                current = (Type) enumerator.Current;
                if (settings.DeserializableConverters[current].GetType() == t)
                {
                    settings.DeserializableConverters.Remove(current);
                    flag = true;
                }
            }
        }

        public static string AjaxID
        {
            get
            {
                return "AjaxPro";
            }
        }

        internal static AjaxSettings Settings
        {
            get
            {
                if (m_Settings != null)
                {
                    return m_Settings;
                }
                lock (m_SettingsLock)
                {
                    if (m_Settings == null)
                    {
                        AjaxSettings section = null;
                        try
                        {
                            section = (AjaxSettings) ConfigurationManager.GetSection("ajaxNet/ajaxSettings");
                        }
                        catch (ConfigurationErrorsException)
                        {
                        }
                        if (section == null)
                        {
                            section = new AjaxSettings();
                            AddDefaultConverter(section);
                        }
                        m_Settings = section;
                    }
                    return m_Settings;
                }
            }
        }
    }
}

